package com.huika.renrench.control.my.merchant.zpic;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.huika.renrench.R;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.my.event.ImgUpEvent;
import com.huika.renrench.control.my.merchant.MyProgramActivity;
import com.huika.renrench.control.my.merchant.zpic.ImageGridAdapter.TextCallback;
import com.huika.renrench.utils.ImageTools;
import com.huika.renrench.view.TitleBarHelper;

import de.greenrobot.event.EventBus;




/**
 * 
 * @description 详细描述：图片选择适配类
 * @author 刘成伟（wwwlllll@126.com）
 * @date 2014-4-18 下午6:34:26
 */
public class ZImageGridAct extends BaseAct {
	public static final String EXTRA_IMAGE_LIST = "imagelist";
	public static final String EXTRA_IMAGE_NUM = "imageNum";
	

	// ArrayList<Entity> dataList;
	List<ImageItem> dataList;
	GridView gridView;
	ImageGridAdapter adapter;
	AlbumHelper helper;
	Button bt;
	private int mImageCount = 5;

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 0:
					Toast.makeText(ZImageGridAct.this, "最多选择" + mImageCount + "张图片", 400).show();
					break;
				default:
					break;
			}
		}
	};


	public void initViews() {
//		setHeadTitle(getString(R.string.image_grid_head));
//		goBack();
		TitleBarHelper titleBarHelper = new TitleBarHelper(this, R.string.onepmall_photoes);
		titleBarHelper.setLeftMsg("");
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		if (mImageCount > 5) {
			adapter = new ImageGridAdapter(ZImageGridAct.this, dataList, mHandler, mImageCount);
		} else {
			adapter = new ImageGridAdapter(ZImageGridAct.this, dataList, mHandler);
		}
		gridView.setAdapter(adapter);
//		adapter.setTextCallback(new TextCallback() {
//			public void onListen(int count) {
//				bt.setText("完成" + "(" + count + ")");
//			}
//		});

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// if(dataList.get(position).isSelected()){
				// dataList.get(position).setSelected(false);
				// }else{
				// dataList.get(position).setSelected(true);
				// }
				// adapter.notifyDataSetChanged();
			}
		});

	}

	public int getmImageCount() {
		return mImageCount;
	}

	public void setmImageCount(int mImageCount) {
		this.mImageCount = mImageCount;
	}

	@Override
	public void setRootView() {
		setContentView(R.layout.activity_image_grid);
		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());
		Bimpz.drrz.remove("");
		dataList = (List<ImageItem>) getIntent().getSerializableExtra(EXTRA_IMAGE_LIST);
		for (ImageItem item : dataList) {
			for (int i = 0; i < Bimpz.drrz.size(); i++) {
				if (MyProgramActivity.imgZurls.get(i).equals(item.imagePath)) {
					item.isSelected = true;
				} 
			}
		}
		mImageCount= getIntent().getIntExtra(EXTRA_IMAGE_NUM, 3);

		initViews();
		bt = (Button) findViewById(R.id.bt);
//		if(Bimpz.drrz.size()>0){
//			bt.setText("完成" + "(" + Bimpz.drrz.size() + ")");
//		}
		bt.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				boolean needAdd = false;
				ArrayList<String> list = new ArrayList<String>();
				List<String> strList = adapter.List;
//				Iterator<String> it = c.iterator();
				for(int i = 0; i < adapter.removeList().size(); i++) {
					String path = adapter.removeList().get(i);
					Bimpz.drrz.remove(MyProgramActivity.imgZurls.indexOf(path));
					MyProgramActivity.imgZurls.remove(path);
				}
				for (int i = 0; i < strList.size(); i++) {
					String path = strList.get(i);
					if(!MyProgramActivity.imgZurls.contains(path)) {
						MyProgramActivity.imgZurls.add(path);
//						EventBus. getDefault().post(new ImgActionEvent(1, 2, path,0));
						needAdd = true;
					}  else {
						needAdd = false;
					}
					File uploadFile = ImageTools.saveImgForUpload(path);
					if (uploadFile != null && uploadFile.exists() && needAdd) {
						list.add(uploadFile.getAbsolutePath());
					}
				}
				for (int i = 0; i < list.size(); i++) {
//					if (Bimp.drrz.size() > 0) {
//						for (int j = 0; j < Bimp.drr.size(); j++) {
//							if (Bimp.drr.get(j).equals(list.get(i))) {
//								Bimp.drr.remove(j);
//							}
//						}
//					}
					if (Bimpz.drrz.size() < mImageCount) {
						Bimpz.drrz.add(list.get(i));
					}
				}
					

				if (Bimpz.act_bool) {
					Intent intent = null;
					if (mImageCount > 10) {
//						intent = new Intent(ImageGridActivity.this, EshopPublishMessageActivity.class);
					} else {
//						intent = new Intent(ImageGridAct.this, PublicShowOrderAct.class);
					}
//					startActivity(intent);
					Bimpz.act_bool = false;
				}
				EventBus. getDefault().post(new ImgUpEvent());
				finish();
				
			}
		});
		initViews();
	}

}
